<?php

/**
 * Created by PhpStorm.
 * User: neilchen
 * Date: 2017/5/7
 * Time: 21:04
 */
namespace common\result;

use common\consts\CommonResources;
use common\consts\StatusCodeEnum;
use Yii;
use yii\base\BaseObject;
use yii\helpers\Json;

/**
 * @property \Exception $exception 异常
// * @property array $sub_results 子结果集合
 * @property boolean $isSuccess 是否成功
 * @property boolean $isSkipped 是否跳过
 * @property string $message 消息
 * @property int $total_count 总记录数
 * @property int $success_count 成功记录数
 * @property int $fail_count 失败记录数
 * @property int $skipped_count 跳过记录数
// * @property array $sub_success_results 成功的子结果集合
// * @property array $sub_fail_results 失败的子结果集合
// * @property array $sub_skipped_results 跳过的子结果集合
 */
class ActionResult extends BaseObject
{
    /**
     * @var boolean
     */
    public $isSuccess;
    /**
     * @var boolean
     */
    public $isSkipped;
    /**
     * @var string
     */
    protected $message;
    /**
     * @var \Exception
     */
    public $exception;
    /**
     * @var int
     */
    public $total_count = 0;
    /**
     * @var int
     */
    public $success_count = 0;
    /**
     * @var int
     */
    public $fail_count = 0;
    /**
     * @var int
     */
    public $skipped_count = 0;
    /**
     * @var boolean
     */
    public $has_sub_result;
//    /**
//     * @var array
//     */
//    public $sub_results;
//    /**
//     * @var array
//     */
//    public $sub_success_results;
//    /**
//     * @var array
//     */
//    public $sub_fail_results;
//    /**
//     * @var array
//     */
//    public $sub_skipped_results;

    /**
     * @param ActionResult $sub_result
     */
    public function addSubResult(ActionResult $sub_result){
//        if (!isset($this->sub_results) || sizeof($this->sub_results) == 0){
//            $this->sub_results = array();
//        }
//        if (!isset($this->sub_success_results) || sizeof($this->sub_success_results) == 0){
//            $this->sub_success_results = array();
//        }
//        if (!isset($this->sub_fail_results) || sizeof($this->sub_fail_results) == 0){
//            $this->sub_fail_results = array();
//        }
//        if (!isset($this->sub_skipped_results) || sizeof($this->sub_skipped_results) == 0){
//            $this->sub_skipped_results = array();
//        }
//
//        if (!isset($sub_result)) return;
//        array_push($this->sub_results, $sub_result);

        if (!isset($sub_result) || empty($sub_result)){
            return;
        }

        if (!$this->has_sub_result){
            $this->has_sub_result = true;
            $this->success_count = $sub_result->success_count;
            $this->fail_count = $sub_result->fail_count;
            $this->skipped_count = $sub_result->skipped_count;
            $this->isSuccess = $sub_result->isSuccess;
        }
        else {
            if ($sub_result->isSkipped){
                $this->skipped_count ++;
            }
            else if ($sub_result->isSuccess){
                $this->success_count ++;
            }
            else {
                $this->fail_count ++;
                $this->isSuccess = false;
            }
        }

//        if ($sub_result->isSkipped){
//            array_push($this->sub_skipped_results, $sub_result);
//        }
//        else {
//            if ($sub_result->isSuccess){
//                array_push($this->sub_success_results, $sub_result);
//            }
//            if (!$sub_result->isSuccess){
//                array_push($this->sub_fail_results, $sub_result);
//            }
//            if($this->isSuccess){
//                $this->isSuccess = $this->isSuccess && $sub_result->isSuccess;
//            }
//        }

        $this->selfComplete();
    }

    /**
     * ActionResult constructor.
     * @param bool $isSuccess
     * @param string $message
     * @param bool $isSkipped
     * @param \Exception|null $exception
     */
    public function __construct($isSuccess = true, $message = '', $isSkipped = false, \Exception $exception = null)
    {
        parent::__construct();
        $this->isSuccess = $isSuccess;
        $this->isSkipped = $isSkipped;
        if (empty($message)){
            $this->message = Yii::t('app', 'No any action executed.');
        }
        else {
            $this->message = $message;
        }

        $this->exception = $exception;

        $this->has_sub_result = false;
        $this->selfInit();
        $this->selfComplete();
    }

    protected function selfInit(){
        $this->success_count = 0;
        $this->fail_count = 0;
        $this->skipped_count = 0;
        if ($this->isSkipped){
            $this->skipped_count = 1;
        }
        else if ($this->isSuccess){
            $this->success_count = 1;
        }
        else {
            $this->fail_count = 1;
            $this->isSuccess = false;
        }
    }

    public function getMessage(){
        if ($this->has_sub_result){
            return CommonResources::RESULT_SUMMARY_MESSAGE($this->total_count, $this->success_count, $this->fail_count, $this->skipped_count);
        }
        return $this->message;
    }
    public function setMessage($message){
        $this->message = $message;
    }

    protected function selfComplete(){
        $this->total_count = $this->success_count + $this->fail_count + $this->skipped_count;

//        $this->message = Yii::t('app', 'TotalCount : {total_count} , Success : {success_count} , Fail : {fail_count} , Skipped : {skipped_count}.', [
//            'total_count' => $this->total_count,
//            'success_count' => $this->success_count,
//            'fail_count' => $this->fail_count,
//            'skipped_count' => $this->skipped_count,
//        ]);

//        if (!isset($this->sub_results) || sizeof($this->sub_results) == 0){
//            $this->total_count = 1;
//
//            if ($this->isSkipped){
//                $this->skipped_count = 1;
//                $this->success_count = 0;
//                $this->fail_count = 0;
//                return;
//            }
//
//            $this->skipped_count = 0;
//            $this->success_count = $this->isSuccess ? 1 : 0;
//            $this->fail_count = $this->total_count - $this->success_count;
//        }
//        else {
//            $this->total_count = count($this->sub_results);
//            $this->success_count = count($this->sub_success_results);
//            $this->fail_count = count($this->sub_fail_results);
//            $this->skipped_count = count($this->sub_skipped_results);
//
//            $this->message = Yii::t('app', 'TotalCount : {total_count} , Success : {success_count} , Fail : {fail_count} , Skipped : {skipped_count}.', [
//                'total_count' => $this->total_count,
//                'success_count' => $this->success_count,
//                'fail_count' => $this->fail_count,
//                'skipped_count' => $this->skipped_count,
//            ]);
//        }
    }

    public function toJson(){
        $code = $this->isSuccess ? StatusCodeEnum::SUCCESS : StatusCodeEnum::BAD_REQUEST;
        $returnArr = [
            'code' => $code,
            'message' => $this->getMessage(),
        ];
        return Json::encode($returnArr);
    }
}